import json

import requests
import re

# basic_url='https://rate.tmall.com/list_detail_rate.htm?itemId=575617865437&spuId=1038280188&sellerId=305358018&order=3&currentPage={}&append=0&content=1'
import time

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys


class ReptleReq:
    def __init__(self):
        self.url = "https://detail.tmall.com/item.htm?spm=a230r.1.14.13.20f345b6bMHqFO&id=586693255679&cm_id=140105335569ed55e27b&abbucket=15"
        self.basePath = "D://dolin"
    def start(self):
        print("——开始自动化爬取———")
        #浏览器设置
        options = webdriver.ChromeOptions()
        # 此步骤很重要，设置为开发者模式，防止被各大网站识别出来使用了Selenium
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        # 不加载图片,加快访问速度
        options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
        #设置中文
        options.add_argument('lang=zh_CN.UTF-8')
        # options.add_argument('--proxy-server=127.0.0.l:8080')
        #更换头部
        ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
        options.add_argument('user-agent="%s"' % ua)
        driver = webdriver.Chrome(chrome_options=options)
        driver.get(self.url)
        assert "东菱" in driver.title
        time.sleep(2)
        driver.execute_script("document.getElementById('sufei-dialog-close').click()")
        #保存cookie
        cookies = driver.get_cookies()
        with open("cookies.txt", "w") as fp:
            json.dump(cookies, fp)
        urls=['https://rate.tmall.com/list_detail_rate.htm?itemId=586693255679&spuId=1143258771&sellerId=3644944659&order=3&currentPage=1&append=0&content=1'.format(i) for i in range(1,11)]
        for url in urls:
            page= get_page(url)
            print(url)
            datas= get_info(page)
            save_data(datas)
def get_page(url):
    try:
        # 也可以使用字典设置,request 读取cookie
        cookies_dict = dict()
        with open("cookies.txt", "r") as fp:
            cookies = json.load(fp)
            for cookie in cookies:
                cookies_dict[cookie['name']] = cookie['value']
        r = requests.get(url,cookies=cookies_dict)
        r.raise_for_status()
        r.encoding='utf-8'
        return r.text
    except Exception as e:
        print(e)

def get_info(page):
    try:
        items=re.findall(r'"rateContent":"(.*?)"',page,re.S)

        for item in items:
            yield item
    except Exception as e:
        print(e)


def save_data(datas):
    print("+++++++++++++++++++")
    with open("D:\\dolin\\淘宝评论.txt","a",encoding="utf-8") as f:
        for data in datas:
            print(data)
            f.write(data)
            f.write('\n')
        f.close()
def writing_set(elem,str,sleepNum):
    for s in str:
        time.sleep(sleepNum);
        elem.send_keys(s)
ReptleReq().start();


###滑动验证码
# while True:
#     try:
#         #定位滑块元素
#         source=driver.find_element_by_xpath("//*[@id='nc_1_n1z']")
#         #定义鼠标拖放动作
#         ActionChains(driver).drag_and_drop_by_offset(source,400,0).perform()
#         #等待JS认证运行,如果不等待容易报错
#         time.sleep(2)
#         #   查看是否认证成功，获取text值
#         text=driver.find_element_by_xpath("//span[@class='nc-lang-cnt']")
#         #目前只碰到3种情况：成功（请在在下方输入验证码,请点击图）；无响应（请按住滑块拖动)；失败（哎呀，失败了，请刷新）
#         if text.text.startswith(u'请在下方'):
#             print('成功滑动')
#             break
#         if text.text.startswith(u'请点击'):
#             print('成功滑动')
#             break
#         if text.text.startswith(u'请按住'):
#             continue
#     except Exception as e:
#         #这里定位失败后的刷新按钮，重新加载滑块模块
#         driver.find_element_by_xpath("//div[@id='nocaptcha']/div/span/a").click()
#         print(e)
#切换iframe
# driver.switch_to_frame("sufei-dialog-content")
# print("开始输入")
# elem = driver.find_element_by_name("TPL_username")  #找到name为loginName的元素，账号框
# elem.clear()  #清空搜索框中的内容
# writing_set(elem,"18312663642",1);
# #输入密码
# elem = driver.find_element_by_name("TPL_password")  #密码框
# elem.clear()  #清空搜索框中的内容
# writing_set(elem,"clq18312663642",1);
# time.sleep(5)
# elem.send_keys(Keys.RETURN)  #相当于回车键，提交
# time.sleep(3);